package com.plian.system.controller.hr.staff;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.Tools.TokenUtil;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.constant.sys.FormStatusConstant;
import com.plian.system.dto.hr.staff.SupervisorDTO;
import com.plian.system.entity.hr.staff.Supervisor;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.hr.staff.IStaffFilesService;
import com.plian.system.service.hr.staff.ISupervisorService;
import com.plian.system.vo.hr.staff.StaffFilesVO;
import com.plian.system.vo.hr.staff.SupervisorVO;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.wrapper.hr.staff.SupervisorWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @description:监事会
 * @author: gangan
 * @create: 2020-03-15 19:51
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("api/supervisor")
public class SupervisorController extends BaseController {

    private ISupervisorService supervisorService;

    private IStaffFilesService staffFilesService;

    private SupervisorWrapper supervisorWrapper;

    /**
     * 详情
     */
    @PostMapping("/detail")
    public R<SupervisorVO> detail(@Valid @RequestBody Supervisor supervisor) {
        Supervisor detail = supervisorService.getOne(Condition.getQueryWrapper(supervisor));
        SupervisorVO supervisorVO = supervisorWrapper.entityToVO(detail);
        return supervisorService.getDetailObj(supervisorVO);
    }

    /**
     * 分页 监事会
     */
    @PostMapping("/list")
    public ListResult<SupervisorVO> list(@Valid @RequestBody Query query) {
        if(query.getAscs()==null&&query.getDescs()==null){
            query.setAscs("supervisor_position_id");
            query.setDescs("create_time");
        }

        List<SearchEntity> entityList = query.getSearch();
        QueryWrapper<Supervisor> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new Supervisor()), entityList
        );
        queryWrapper.eq("create_company_id", TokenUtil.getTokenOrgId());

        IPage<Supervisor> pages = supervisorService.page(Condition.getPage(query), queryWrapper);
        List<SupervisorVO> supervisorVOS = supervisorWrapper.entityToVO(pages.getRecords());
        ListResult<SupervisorVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(supervisorVOS);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;

    }

    /**
     * 新增或修改 监事会
     */
    @PostMapping("/submit")
    @Resubmit(delaySeconds = 0)
    @LogOperation("监事会档案-提交")
    public R submit(@Valid @RequestBody Supervisor supervisor) {
        String checkResult = "";
        StaffFilesVO staffFilesVO = supervisor.getStaffFilesVO();
        if (!(Optional.ofNullable(staffFilesVO.getStatus()).isPresent() && staffFilesVO.getStatus() == FormStatusConstant.STASH)){
            checkResult = staffFilesService.checkIsExisted(staffFilesVO.getId(),staffFilesVO.getStaffCode(),staffFilesVO.getCompanyNameId());
        }

        if(StringUtil.isBlank(checkResult)) {
            if (!staffFilesService.checkDocumentNumber(staffFilesVO.getId(), staffFilesVO.getCompanyNameId(), staffFilesVO.getDocumentNumber())) {
                return new R(CustomCode.builder().message("证件号已存在").code(1999).success(false).build());
            }
        }
        return supervisorService.submit(supervisor) ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }


    /**
     * 删除 监事会
     */
    @PostMapping("/remove")
    @LogOperation("监事会档案-删除")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return supervisorService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    /**
     *
     * @param supervisorDTO
     * @return
     */
    @PostMapping("/report")
    public ObjResult reportTable(@RequestBody SupervisorDTO supervisorDTO) {
        ReportReturnVO reportReturnVO = supervisorService.report(supervisorDTO);
        ObjResult objResult = new ObjResult(CommonCode.SUCCESS);
        if (Optional.ofNullable(reportReturnVO).isPresent()){
            PageData pd = new PageData();
            pd.put("grids", reportReturnVO.getGrids());
            objResult.setPageData(pd);
        }
        return objResult;

    }

}
